﻿-- ================================================
-- Remarks: 
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Ivy_Flat_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Ivy_Flat_Update]
GO



CREATE PROCEDURE [Ivy_Flat_Update] 
-- =============================================
-- Author:		<Sagar sk>
-- Create date: <12/08/2014>
-- Description:	<Ivy_Flat_Update>
-- Modified by:		<Mrinmoy Das>
-- Modified date:	<27/10/2014>
-- =============================================
-- Add the parameters for the stored procedure here
	(
				@FlatKey int,
				@FlatSocietyKey int,
				@FlatNumber varchar(50),
				@FlatFloorNumber varchar(50),
				@FlatType varchar(50) = NULL,
				@FlatBlockName varchar(50),
				@FlatOccupancy int,
				@FlatNumberOfBedrooms int = NULL,
				@FlatNumberOfKitchen int = NULL,
				@FlatNumberOfToilets int = NULL,
				@FlatNumberOfLivingroom int = NULL,
				@FlatNumberOfStudyroom int = NULL,
				@FlatNumberOfBalcony int = NULL,
				@FlatTotalArea int = NULL,
				@FlatNumberOfWaterInlet int = NULL,
	
	         	@Sys_ModifiedByID varchar(50) = NULL,
				@Sys_DeleteType int = 0,
				@Sys_IsDeleted bit = false,
				@Sys_OrganizationID int = NULL,
				@Sys_InitialPrimaryKeys varchar(max)= NULL,
				@Sys_WorkstationName varchar(50)= NULL,
				@ConcurrencyTimeStamp datetime = NULL,

				@Sys_DateOfModification datetime OUTPUT
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT OFF;

	-- ConcurrencyTimeStamp is mandatory and it needs to check if any other user has changed the reord or not	
	IF @ConcurrencyTimeStamp = NULL
	BEGIN
		RAISERROR('Missing @ConcurrencyTimeStamp', 11,1);
	END
	DECLARE @CurrentModificationDate datetime;
	SET @CurrentModificationDate = (SELECT [Sys_DateOfModification] FROM [Flat] WHERE [FlatKey] = @FlatKey);
	If @ConcurrencyTimeStamp != @CurrentModificationDate
	BEGIN
		RAISERROR('Record is Changed by another process', 11,1);
	END
	
	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END
	DECLARE @CurrentDataTime datetime;
	SET @CurrentDataTime = GETDATE();
	
    UPDATE	[Flat]
	SET 
			    
				[FlatSocietyKey] = @FlatSocietyKey,
				[FlatNumber] = @FlatFloorNumber,
				[FlatFloorNumber] = @FlatFloorNumber,
				[FlatType] = @FlatType,
				[FlatBlockName] = @FlatBlockName,
				[FlatOccupancy] = @FlatOccupancy,
				[FlatNumberOfBedrooms] = @FlatNumberOfBedrooms,
				[FlatNumberOfKitchen] = @FlatNumberOfKitchen,
				[FlatNumberOfToilets] = @FlatNumberOfToilets,
				[FlatNumberOfLivingroom] =@FlatNumberOfLivingroom,
				[FlatNumberOfStudyroom] =@FlatNumberOfStudyroom,
				[FlatNumberOfBalcony] =@FlatNumberOfBalcony,
				[FlatTotalArea] =@FlatTotalArea,
				[FlatNumberOfWaterInlet] = @FlatNumberOfWaterInlet,
				

				[Sys_DateOfModification] = @CurrentDataTime,
				[Sys_ModifiedByID] = @Sys_ModifiedByID,
				[Sys_DeleteType] = @Sys_DeleteType,
				[Sys_IsDeleted] = @Sys_IsDeleted,
				[Sys_OrganizationID] = @Sys_OrganizationID,
				[Sys_InitialPrimaryKeys] = @Sys_InitialPrimaryKeys,
				[Sys_WorkstationName] = @Sys_WorkstationName
	 WHERE
		(
			[FlatKey] = @FlatKey 
		);
		SET @Sys_DateOfModification = @CurrentDataTime;

END

GO


